package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.AppParam;
import com.dmap.admin.controller.backgroundv2.param.AppSelectParam;
import com.dmap.admin.dao.AppMapper;
import com.dmap.admin.po.App;
import com.dmap.admin.po.AppExample;
import com.dmap.admin.po.Company;
import com.dmap.admin.po.Project;
import com.dmap.admin.service.backgroundv2.AppService;
import com.dmap.admin.service.backgroundv2.CompanyService;
import com.dmap.admin.service.backgroundv2.ProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 25/05/18
 * \* Description:app相关接口实现类
 * \
 */
@Service
public class AppServiceImpl implements AppService {

    @Resource
    private AppMapper appMapper;
    @Resource
    private ProjectService projectService;
    @Resource
    private CompanyService companyService;

    @Override
    public App insertApp(AppParam param) {
        App app = new App();
        app.setProjectId(param.getProjectId());
        app.setProjectName(param.getProjectName());
        app.setAppImageUrl(param.getAppImageUrl());
        app.setStatus(param.getStatus());
        app.setNote(param.getNote());
        app.setCreateTime(System.currentTimeMillis() / 1000);
        app.setUpdateTime(app.getCreateTime());
        int success = appMapper.insertSelective(app);
        return (success == 1) ? app : null;
    }

    @Override
    public boolean updateApp(AppParam param) {
        App app = new App();
        app.setId(Long.valueOf(param.getId()));
        app.setProjectId(param.getProjectId());
        app.setProjectName(param.getProjectName());
        app.setAppImageUrl(param.getAppImageUrl());
        app.setStatus(param.getStatus());
        app.setNote(param.getNote());
        app.setUpdateTime(System.currentTimeMillis() / 1000);
        return appMapper.updateByPrimaryKeySelective(app) == 1;
    }

    @Override
    public boolean validateProjectIdOrNameIsExist(String projectId, String projectName) {
        boolean returnValue = true;
        AppExample example = new AppExample();
        //匹配 项目编号 进行查询
        AppExample.Criteria criteria1 = example.createCriteria();
        //匹配 项目名称 进行查询
        AppExample.Criteria criteria2 = example.createCriteria();
        if (StringUtils.isNotBlank(projectId)) {
            criteria1.andProjectIdEqualTo(projectId);
        }
        if (StringUtils.isNotBlank(projectName)) {
            criteria2.andProjectNameEqualTo(projectName);
        }
        example.or(criteria2);
        List<App> list = appMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            returnValue = false;
        }
        return returnValue;
    }

    @Override
    public App selectById(Long id) {
        return appMapper.selectByPrimaryKey(id);
    }

    @Override
    public App selectByProjectId(String projectId) {
        AppExample example = new AppExample();
        example.createCriteria().andProjectIdEqualTo(projectId);
        List<App> list = appMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<App> selectByCompanyId(Long companyId) {
        Company company = companyService.selectById(companyId);
        AppExample example = new AppExample();
        //总公司则返回所有app, 分公司则查询其拥有的app
        if (company.getCompanyType() != AdminConstant.COMPANY_TYPE_HEAD) {
            List<Project> projectList = projectService.selectByCompanyId(companyId);
            if (CollectionUtils.isEmpty(projectList)) {
                return Collections.emptyList();
            }
            List<Long> appIdList = new ArrayList<>();
            for (Project project : projectList) {
                if (project != null && project.getAppId() != null && !project.getAppId().equals(0L)) {
                    appIdList.add(project.getAppId());
                }
            }
            if (CollectionUtils.isEmpty(appIdList)) {
                return Collections.emptyList();
            }
            example.createCriteria().andIdIn(appIdList);
        }
        int pageSize = appMapper.countByExample(example);
        if (pageSize < 1) {
            return Collections.emptyList();
        }
        example.setLimitStart(0);
        example.setLimitEnd(pageSize);
        return appMapper.selectByExample(example);
    }

    @Override
    public List<App> selectByAppSelectParam(AppSelectParam param) {
        AppExample example = this.getAppSelectParamExample(param.getSearchKeyword());
        //根据前端所传参数不同生成对应的orderBy子句
        String orderByClause = AdminConstant.getOrderByClauseMap().getOrDefault(param.getSortField(), AdminConstant.UPDATE_TIME)
                + AdminConstant.getOrderByClauseSequenceMap().getOrDefault(param.getOrder(), AdminConstant.DESC);
        example.setOrderByClause(orderByClause);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return appMapper.selectByExample(example);
    }

    @Override
    public int countByAppSelectParam(AppSelectParam param) {
        AppExample example = this.getAppSelectParamExample(param.getSearchKeyword());
        return appMapper.countByExample(example);
    }

    private AppExample getAppSelectParamExample(String searchKeyword) {
        AppExample example = new AppExample();
        //根据 projectId 模糊搜索
        AppExample.Criteria criteria1 = example.createCriteria();
        //根据 projectName 模糊搜索
        AppExample.Criteria criteria2 = example.createCriteria();
        if (StringUtils.isNotBlank(searchKeyword)) {
            criteria1.andProjectIdLike("%" + searchKeyword + "%");
            criteria2.andProjectNameLike("%" + searchKeyword + "%");
        }
        example.or(criteria2);
        return example;
    }

    /**
     * 查询所有的projectId
     *
     * @return 所有的projectId的list
     */
    @Override
    public List<String> selectAllProjectId() {
        return appMapper.selectAllProjectId();
    }

}